package com.zqh.web.manager;



import cn.hutool.core.collection.CollUtil;

import com.volcengine.ark.runtime.model.completion.chat.ChatCompletionChoice;
import com.volcengine.ark.runtime.model.completion.chat.ChatCompletionRequest;
import com.volcengine.ark.runtime.model.completion.chat.ChatMessage;
import com.volcengine.ark.runtime.model.completion.chat.ChatMessageRole;
import com.volcengine.ark.runtime.service.ArkService;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


/**
 * @author zqh
 * @date 2025/5/07 0029 15:05
 * AI调用工具类
 */

@Configuration
@Data
@RequiredArgsConstructor
public class AiManager {


    private final ArkService arkService;


    public String doChat(String systemPrompt, String userPrompt) {

        System.out.println("请求开始");
        final List<ChatMessage> messages = new ArrayList<>();
        final ChatMessage systemMessage = ChatMessage.builder().role(ChatMessageRole.SYSTEM).content(systemPrompt).build();
        final ChatMessage userMessage = ChatMessage.builder().role(ChatMessageRole.USER).content(userPrompt).build();
        messages.add(systemMessage);
        messages.add(userMessage);

        return doChat(messages);
    }

    public String doChat(List<ChatMessage> messages) {
        ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest.builder()
                // 指定您创建的方舟推理接入点 ID，此处已帮您修改为您的推理接入点 ID
                .model("ep-20250506233750-t9fz7")
                .messages(messages)
                .build();

        List<ChatCompletionChoice> choicesList = arkService
                .createChatCompletion(chatCompletionRequest)
                .getChoices();

        if (CollUtil.isEmpty(choicesList)){
            throw new RuntimeException("Ai没有返回内容");
        }

        String content = (String) choicesList.get(0).getMessage().getContent();
        System.out.println("Ai返回内容：" + content);

        return content;

    }



}
